“Files Used:”

Z:/COVID-19_WastewaterAnalysis/data/processed/MMSD_Interceptor_Cases_2_7_22.csv

Z:/COVID-19_WastewaterAnalysis/data/processed/LIMSWasteData_02-09-22.csv

SizeUsed = 1
alphaUsed = .9
N1ShapeUnit = 4
N2ShapeUnit = 5
HeightJit <- 0
WidthJit <- 0


MadDF <- filter(FullDF,Site=="Madison")%>%
    mutate(FlagedOutliersN1 = IdentifyOutliers(N1,Bin = 21, Action = "Flag"),
           FlagedOutliersN2 = IdentifyOutliers(N2,Bin = 28, Action = "Flag"),
           NoOutlierVarN1 = ifelse(FlagedOutliersN1, NA, N1),
           NoOutlierVarN2 = ifelse(FlagedOutliersN2, NA, N2),)


NonOuliersDF <- MadDF%>%
  mutate(Outlier = ifelse(FlagedOutliersN1,N1,NA))%>%
           mutate(N1 = NoOutlierVarN1,
                  N2 = NoOutlierVarN2)

OutLierPlotDF <- MadDF%>%
  mutate(OutlierN1 = ifelse(FlagedOutliersN1,N1,NA),
         OutlierN2 = ifelse(FlagedOutliersN2,N2,NA))%>%
           mutate(N1 = NoOutlierVarN1,
                  N2 = NoOutlierVarN2)%>%
  #filter(!(is.na(N1)&is.na(Outlier)))%>%
  ggplot(aes(x=Date))+#Data depends on time 
  geom_jitter(aes(y=N1, color="inlier", info = N1), 
             data=NonOuliersDF, size = .5, shape = N1ShapeUnit, stroke = .2,
             width = WidthJit ,height = HeightJit )+
  geom_jitter(aes(y=N2, color="inlier",  info = N2), 
             data=NonOuliersDF, size = .5, shape = N2ShapeUnit, stroke = .2,
             width = WidthJit ,height = HeightJit )+
  geom_jitter(aes(y=OutlierN1, color="Outlier", info = OutlierN1),
             shape=N1ShapeUnit, width = WidthJit ,height = HeightJit )+
  geom_jitter(aes(y=OutlierN2, color="Outlier", info = OutlierN2),
             shape=N2ShapeUnit, width = WidthJit ,height = HeightJit )+
  theme_light() +
  #scale_y_log10()+
  scale_color_manual(values=c("#F8766D","#999999"))

ggplotly(OutLierPlotDF, tooltip = c("x","y","colour"))%>%
  config(modeBarButtonsToRemove = c("zoomIn2d", "zoomOut2d","lasso2d",
                                    "select2d", "autoScale2d"))%>%
  config(displaylogo = FALSE)
#"2021-06-08","2021-10-17","2021-05-02","2021-01-26"
IntercepterDF <- FullDF %>%
  group_by(Site)%>%
  mutate(FudgeFactor = mean(N1))%>%#Mean of sites to see if it works as normalizer
  filter(Site != "Madison")


RotSpacing <- seq(15, 375, length = 6)
HueSpace <- hcl(h = RotSpacing, l = 65, c = 100)[1:5]
HueSpace <- c(tail(HueSpace, -3), head(HueSpace, 3))
IntercepterOverLay <- IntercepterDF%>%
  filter(Date>mdy("1/1/2021"))%>%
  ggplot(aes(x=Date))+
  geom_point(aes(y=N1,color = Site),size = SizeUsed, alpha= alphaUsed,shape=N1ShapeUnit)+
  geom_point(aes(y=N2,color = Site),size = SizeUsed, alpha= alphaUsed,shape=N2ShapeUnit)+
  theme_light()+
  scale_colour_manual(values = HueSpace)+
  scale_y_log10()

ggplotly(IntercepterOverLay)%>%
  config(modeBarButtonsToRemove = c("zoomIn2d", "zoomOut2d","lasso2d",
                                    "select2d", "autoScale2d"))%>%
  config(displaylogo = FALSE)
#Get P18 to pop out
IntercepterChangeDF <- IntercepterDF%>%
  filter(!is.na(N1))%>%
  mutate(ChangeN1 = lead(N1) - N1,
         ChangeN2 = lead(N2) - N2,
         PerChangeN1 = log(lead(N1) - N1),
         PerChangeN2 = log(lead(N2) - N2))


IntercepterChangeOverLay <- IntercepterChangeDF%>%
  ggplot(aes(x=Date))+
  geom_point(aes(y = ChangeN1, color = Site), size = SizeUsed,
             alpha = alphaUsed, shape=N1ShapeUnit)+
  geom_point(aes(y = ChangeN2,color = Site), size = SizeUsed,
             alpha = alphaUsed, shape = N2ShapeUnit)+
  scale_colour_manual(values = HueSpace)+
  theme_light()#+
  #scale_y_log10()

ggplotly(IntercepterChangeOverLay)%>%
  config(modeBarButtonsToRemove = c("zoomIn2d", "zoomOut2d","lasso2d",
                                    "select2d", "autoScale2d"))%>%
  config(displaylogo = FALSE)
IntercepterPerChangeOverLay <- IntercepterChangeDF%>%
  ggplot(aes(x = Date))+
  geom_point(aes(y = PerChangeN1, color = Site), size = SizeUsed,
             alpha = alphaUsed, shape = N1ShapeUnit)+
  geom_point(aes(y = PerChangeN2, color = Site), size = SizeUsed,
             alpha= alphaUsed,shape = N2ShapeUnit)+
  theme_light()#+
  #scale_y_log10()

#ggplotly(IntercepterPerChangeOverLay)
LoessFunc <- function(SiteFilter,DF,SpanConstant = .163){
  MainDF <- DF%>%
    filter(Site==SiteFilter)
  MainDF[["loessN1"]] <- loessFit(y=(MainDF[["N1"]]), 
                      x=MainDF$Date, #create loess fit of the data
                      span=SpanConstant, #span of .2 seems to give the best result, not rigorously chosen
                      iterations=5)$fitted#2 iterations remove some bad patterns
return(MainDF)
}

SiteLoessDF <- lapply(c("MMSD-P11","MMSD-P18","MMSD-P2","MMSD-P7","MMSD-P8"),
                      LoessFunc,IntercepterDF,SpanConstant=.2)%>%
                bind_rows()

BaseColDF <- SiteLoessDF%>%
  mutate(Norm = "None",
         Val = loessN1,
         islog = FALSE)

FlowColDF <- SiteLoessDF%>%
  mutate(Norm = "Flow",
         Val = loessN1*FlowRate,
         islog = FALSE)

PopColDF <- SiteLoessDF%>%
  mutate(Norm = "Pop",
         Val = loessN1/Pop,
         islog = FALSE)

MergedToBeFacetedDF <- rbind(BaseColDF, FlowColDF, PopColDF)


NonLogPlotlyPlot <- ggplotly( MergedToBeFacetedDF%>%
            ggplot(aes(x=Date))+
            geom_line(aes(y=Val, color = Site))+
            facet_wrap(~Norm, scales = "free_y",nrow=3))

LogPlotlyPlot <- ggplotly( MergedToBeFacetedDF%>%
            ggplot(aes(x=Date))+
            geom_line(aes(y=Val, color = Site))+
            scale_y_log10()+
            facet_wrap(~Norm, scales = "free_y",nrow=3))

NonLogPlotlyPlot
LogPlotlyPlot
Mean <- 11.73
StandardDeviation <- 7.68
Scale = StandardDeviation^2/Mean
Shape = Mean/Scale
SLDWidth <- 21
weights <- dgamma(1:SLDWidth, scale = Scale, shape = Shape)

SiteLoessDF <- FullDF%>%
  filter(Site!="Madison")

A <- SiteLoessDF%>%
  filter(!is.na(SevenDayMACases),
         SevenDayMACases != 0)%>%
  ggplot(aes(x=Date))+
  geom_point(aes(y = FirstConfirmed, color = Site), 
             data = IntercepterDF, size = .5, alpha = .5)+
  geom_line(aes(y = SevenDayMACases, color = Site))+
  scale_colour_manual(values = HueSpace)+
  #geom_line(aes(y = SLDCases, color = Site))+
  scale_y_log10()
ggplotly(A)%>%
  config(modeBarButtonsToRemove = c("zoomIn2d", "zoomOut2d","lasso2d",
                                    "select2d", "autoScale2d"))%>%
  config(displaylogo = FALSE)
B <- SiteLoessDF%>%
  filter(!is.na(SevenDayMACases),
         SevenDayMACases != 0)%>%
  ggplot(aes(x=Date))+
  geom_point(aes(y = FirstConfirmed / Pop, color = Site), 
             data = IntercepterDF, size = .5, alpha = .5)+
  geom_line(aes(y = SevenDayMACases / Pop, color = Site))+
  scale_colour_manual(values = HueSpace)+
  #geom_line(aes(y = SLDCases / Pop, color = Site))+
  scale_y_log10()
ggplotly(B)%>%
  config(modeBarButtonsToRemove = c("zoomIn2d", "zoomOut2d","lasso2d",
                                    "select2d", "autoScale2d"))%>%
  config(displaylogo = FALSE)